var app = angular.module('conversionTool', ['smart-table']);

app.controller('viewDataCtrl', [ '$scope', '$http', function($scope, $http) {

	$scope.dataAdaptors;

	$scope.taskId;

	$scope.filter = true;

	$scope.formula = true;
	
	$scope.colspan=0;

	$scope.header = new Array();

	$scope.content = new Array();
	
	$scope.contentDisplay = new Array();

	$scope.initialTaskId = function(id) {
		if (id != "") {
			$scope.taskId = id;

			$scope.getDataAdaptor();
		} else {
			$scope.msg = "server error, please refresh!";
		}
	}
	
	$scope.back=function(){
		
		window.history.back();
	}
	

	$scope.getDataAdaptor = function() {

		$http.post('/rest/task/getTaskDataAdaptor', {
			"taskId" : $scope.taskId,
			"filter" : $scope.filter,
			"formula" : $scope.formula
		}).success(function(data) {
			 
			if (data != undefined && data != "") {

				if (data[0].header != undefined) {
					$scope.header = data[0].header;
				}
				$scope.content = new Array();
				data.forEach(function(entry) {
					entry.content.forEach(function(item) {
						$scope.content.push(item);
					});
				});
				
				$scope.contentDisplay=[].concat($scope.content);  
				$scope.colspan=$scope.header.length;

				$scope.dataAdaptors = data;

			} else {
				$scope.msg = "file not mapping or sever error";
			}

		}).error(function(data) {
			console.log(data);
			$scope.msg = "sever error";
		});
	}

} ]);
